package com.example.demo.Controller;

import com.example.demo.Entity.OPeration_Statistics;
import com.example.demo.Entity.Operation;
import com.example.demo.Entity.Statistics;
import com.example.demo.Server.operationServer;
import com.example.demo.Server.statisticsServer;
import com.example.demo.Tools.Modification;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class OperationController {
    @Autowired
    private operationServer server;
    @Autowired
    statisticsServer dao;
    @Autowired
    Operation operation;

    @Autowired
    Gson gson;
    /************ 查询所有操作记录 ***************/

    @ResponseBody
    @CrossOrigin
    @RequestMapping(value = "/findAllOperation")
    public List<Operation> data(){
        //查询所以记录
        return server.findAllOperation();
    }
    /************ 添加操作记录 ***************/
    @ResponseBody
    @RequestMapping(value = "/AddOperation",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    @CrossOrigin
    public String AddOperation(@RequestBody String data){
        Operation operation = gson.fromJson(data,Operation.class);
        //保存用户的操作记录
        server.AddOperation(operation);
        //返回新添加账务信息的编号
        return "success";
    }
    /************ 添加修改的操作记录 ***************/
    @ResponseBody
    @RequestMapping(value = "/AddQueryOperation",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    @CrossOrigin
    public String AddQueryOperation(@RequestBody String data){

        //获取修改前的账务信息和操作记录信息
        OPeration_Statistics os = gson.fromJson(data,OPeration_Statistics.class);
        //获取修改后的账务信息
        Statistics sta = dao.findByID(os.getStatisticsId());
        //设置值
        operation.setTime(os.getTime());
        operation.setUserid(os.getUserid());
        operation.setStatisticsId(os.getStatisticsId());
        operation.setType(os.getType());
        operation.setDetailed(Modification.ModUpdate(os,sta));

        server.AddOperation(operation);
        return "success";
    }

    /************ 删除操作记录 ***************/
    @ResponseBody
    @RequestMapping("/DeleteOperation")
    public Integer DeleteOperation(Operation operation){
        return server.DeleteOperation(operation) == 1? 1 : 0;
    }

    /************ 修改操作记录 ***************/
    @ResponseBody
    @RequestMapping("/UpdateOperation")
    public Integer UpdateOperation(Operation operation){

        return server.UpdateOperation(operation) == 1? 1 : 0;
    }

    /*****
     *
     *
     * 进行全表的模糊查询
     *
     * ******/
    @ResponseBody
    @RequestMapping(value = "/QueryOperation",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    @CrossOrigin
    public List<Operation> QueryOperation(@RequestBody String query) {
        //前端传过来的是json数据，需要截取，因为单传一个字符串中文会乱码，暂时没有办法解决
        //将前端传来的json数据截取带分号的数据
        String data = query.substring(9,query.length()-1);
        //将前端传来的json数据截取不带分号的数据
        String data2 = query.substring(10,query.length()-2);
        //条件表达式  将前端传来的json数据截取成需要的数据,判断是否存在 存在返回查询的数据 不存在返回所有数据
        return data.equals("\"\"")? server.findAllOperation():server.QueryOperation(data2);
    }

    /*****
     *
     *
     * 进行全表的模糊查询
     *
     * ******/
    @ResponseBody
    @RequestMapping(value = "/test")
    @CrossOrigin
    public String test() {

        return "success2018";
    }


}
